package com.feri.fss.service.impl;

import com.feri.fss.common.vo.PageBean;
import com.feri.fss.common.vo.R;
import com.feri.fss.dao.AdminDao;
import com.feri.fss.entity.Admin;
import com.feri.fss.service.intf.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service //IOC创建对象
public class AdminServiceImpl implements AdminService {
    @Autowired //为属性赋值，从IOC对象
    private AdminDao dao;

    @Override
    public R login(String name, String password, HttpSession session) {
        //对参数进行校验
        if(name!=null && password!=null && name.length()>0 && password.length()>0){
            //查询数据库，用传递的用户名
            Admin admin = dao.selectByName(name);
            if (admin!=null){
                if (admin.getFlag()==1){
                    //验证用户是否有效
                    if (admin.getPassword().equals(password)){
                        //登录成功,存储当前用户到Session
                        session.setAttribute("curruser",admin);
                        return R.ok();
                    }else {
                        return R.fail("密码错误");
                    }
                }else {
                    return R.fail("账号异常，请联系客服");
                }
            }else {
                return R.fail("账号不存在");
            }
        }else {
            return R.fail("请输入账户信息");
        }

    }

    @Override
    public R save(Admin admin) {
        if (dao.insert(admin)>0){
            return R.ok();
        }else {
            return R.fail("网络繁忙，请稍后再试！");
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
        //开启分页，设置分页信息
        PageHelper.startPage(page, limit);
        //获取分页查询的结果
        PageInfo<Admin> pageInfo = new PageInfo<>(dao.selectAll());
        //返回结果
        return new PageBean(pageInfo.getTotal(),pageInfo.getList());
    }
}
